<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-alert
        title="添加商品信息"
        type="info"
        center
        show-icon
        :closable="false"
      > </el-alert>
      <!-- 步骤条 -->
      <el-steps
        :space="200"
        :active="activeIndex-0"
        finish-status="success"
        align-center
      >
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>

      <!-- tab栏 -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
      >
        <el-tabs
          :tab-position="tabPosition"
          v-model="activeIndex"
          :before-leave="tabsChangehandle"
          @tab-click="tabsChangeCheck"
        >
          <!-- // 商品基本信息 -->
          <el-tab-pane
            label="基本信息"
            name="0"
          >
            <el-form-item
              label="商品名称"
              prop="goods_name"
            >
              <el-input v-model="ruleForm.goods_name"></el-input>
            </el-form-item>
            <el-form-item
              label="商品价格"
              prop="goods_price"
            >
              <el-input
                v-model="ruleForm.goods_price"
                type="number"
              ></el-input>
            </el-form-item>
            <el-form-item
              label="商品数量"
              prop="goods_number"
            >
              <el-input
                v-model="ruleForm.goods_number"
                type="number"
              ></el-input>
            </el-form-item>
            <el-form-item
              label="商品重量"
              prop="goods_weight"
            >
              <el-input
                v-model="ruleForm.goods_weight"
                type="number"
              ></el-input>
            </el-form-item>
            <!-- 分级选择器区域 -->
            <el-form-item
              label="商品分类"
              prop="goods_cat"
            >
              <el-cascader
                v-model="ruleForm.goods_cat"
                :options="parParentOptions"
                :props="paramsProps"
                @change="handleParchanged"
                clearable
              ></el-cascader>
            </el-form-item>
          </el-tab-pane>
          <!-- // 商品参数 -->
          <el-tab-pane
            label="商品参数"
            name="1"
          >
            <el-form-item
              v-for="item in goodsParams"
              :key="item.attr_id"
              :label="item.attr_name"
            >
              <el-checkbox-group v-model="item.attr_vals">
                <el-checkbox
                  :label="cb"
                  v-for="(cb,i) in item.attr_vals"
                  :key="i"
                  border
                ></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-tab-pane>
          <!-- //商品属性 -->
          <el-tab-pane
            label="商品属性"
            name="2"
          >
            <el-form-item
              :label="item.attr_name"
              v-for="item in onlyTableList"
              :key="item.attr_id"
            >
              <el-input
                v-model="item.attr_vals"
                disabled
              ></el-input>
            </el-form-item>
          </el-tab-pane>
          <!-- // 商品图片 -->
          <el-tab-pane
            label="商品图片"
            name="3"
          >
            <el-upload
              action="https://lianghj.top:8888/api/private/v1/upload"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :on-success="handleSuccess"
              list-type="picture"
              :headers="headObj"
            >
              <el-button
                size="small"
                type="primary"
              >点击上传</el-button>
              <div
                slot="tip"
                class="el-upload__tip"
              >只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
          </el-tab-pane>
          <!-- // 商品内容 -->
          <el-tab-pane
            label="商品内容"
            name="4"
          >商品内容</el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>

    <!-- 图片预览弹窗 -->
    <el-dialog
      title="图片预览"
      :visible.sync="picDialogVisible"
      width="50%"
    >
      <span><img
          :src="baseUrl+pic_path"
          alt=""
        ></span>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="picDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      activeIndex: '0',
      tabPosition: 'left',
      // 表单数据
      ruleForm: {
        goods_name: '', // 商品名称
        goods_price: 0, // 商品价格
        goods_number: 0, // 商品数量
        goods_weight: 0, // 商品重量
        goods_cat: [], // 商品分类
        pics: [] // 图片临时路径
      },
      rules: {
        goods_name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
        goods_price: [{ required: true, message: '请输入商品价格', trigger: 'blur' }],
        goods_number: [{ required: true, message: '请输入商品数量', trigger: 'blur' }],
        goods_weight: [{ required: true, message: '请输入商品重量', trigger: 'blur' }]
      }, // 验证规则
      // 分级选择器本地数据
      parParentOptions: [],
      paramsProps: {
        expandTrigger: 'hover',
        checkStrictly: true,
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      // 商品参数本地数据
      goodsParams: [],
      // 静态参数本地数据
      onlyTableList: [],
      picDialogVisible: false,
      // 上传图片需要的请求头
      headObj: {
        Authorization: window.sessionStorage.getItem('token')
      },
      // 图片预览时的大图路径
      baseUrl: 'https://lianghj.top:8888/api/private/v1/',
      pic_path: ''
    }
  },
  created () {
    this.getParamsList()
  },
  methods: {
    handleParchanged () {
      if (this.ruleForm.goods_cat.length !== 3) {
        this.ruleForm.goods_cat = []
      }
    },
    // 获取分级选择器需要的数据
    async getParamsList () {
      const { data: res } = await this.$http.get('categories')
      if (res.meta.status !== 200) return this.$message.error('获取分类数据失败')
      // console.log(res)
      this.parParentOptions = res.data
    },
    tabsChangehandle (activeName, oldActiveName) {
      if (oldActiveName === '0' && this.ruleForm.goods_cat.length !== 3) {
        this.$message.error('请先选择商品分类')
        return false
      }
    },
    async tabsChangeCheck () {
      if (this.activeIndex === '1') {
        const { data: res } = await this.$http.get(`categories/${this.cateID}/attributes`, {
          params: {
            sel: 'many'
          }
        })
        res.data.forEach(item => {
          item.attr_vals = item.attr_vals.length === 0 ? [] : item.attr_vals.split(' ')
        })
        this.goodsParams = res.data
      } else if (this.activeIndex === '2') {
        const { data: res } = await this.$http.get(`categories/${this.cateID}/attributes`, {
          params: {
            sel: 'only'
          }
        })
        this.onlyTableList = res.data
        console.log(res)
      }
    },
    // 处理图片预览事件
    handlePreview (file) {
      console.log(file)
      this.pic_path = file.response.data.tmp_path
      console.log(this.pic_path)
      this.picDialogVisible = true
    },
    // 处理移除图片事件
    handleRemove (file) {
      console.log(file)
      const removeInfo = file.response.data.tmp_path
      const i = this.ruleForm.pics.findIndex(x => x.pic === removeInfo)
      this.ruleForm.pics.splice(i, 1)
      console.log(this.ruleForm)
    },
    // 图片上传成功后获得临时路径并存 放到本地数据
    handleSuccess (response) {
      const picInfo = { pic: response.data.tmp_path }
      this.ruleForm.pics.push(picInfo)
    }
  },
  computed: {
    cateID () {
      if (this.ruleForm.goods_cat.length === 3) {
        return this.ruleForm.goods_cat[2]
      }
      return null
    }
  }

}
</script>

<style scoped>
</style>
